import CryptoJS from 'crypto-js'

// 默认的 KEY 与 iv 如果没有给
const KEY = CryptoJS.enc.Utf8.parse('bcb966ecbc60cdd8edff36352fc79403') // ""中与后台一样  密码
/**
 * AES加密 ：字符串 key 返回base64
 */
export function Encrypt(word, keyStr) {
  let key = KEY
  if (keyStr) {
    key = CryptoJS.enc.Utf8.parse(keyStr)
  }
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  // console.log("-=-=-=-", encrypted.ciphertext)
  const Base64 = require('js-base64').Base64
  return CryptoJS.enc.Utf8.stringify(Base64.encode(encrypted)).toString()
}

/**
 * AES 解密 ：字符串 key 返回base64
 *
 */
export function Decrypt(word, keyStr) {
  // const Base64 = require('js-base64').Base64
  let key = KEY
  // const Base64 = require('js-base64').Base64
  // const words = Base64.decode(word)

  if (keyStr) {
    key = CryptoJS.enc.Utf8.parse(keyStr)
  }
  const src = CryptoJS.enc.Utf8.parse(word)
  const decrypt = CryptoJS.AES.decrypt(src, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}
